package com.aletto.general_store.ui.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.aletto.general_store.R;
import com.aletto.general_store.data.DataRepository;
import com.aletto.general_store.data.model.Goods;
import com.aletto.general_store.data.model.Store;
import com.aletto.general_store.presenters.SearchGoodsPageTemplate;
import com.aletto.general_store.presenters.StoreGoodsPageTemplate;
import com.aletto.general_store.presenters.core.SimpleParamsTemplate;
import com.aletto.general_store.presenters.core.SimpleTemplate;
import com.aletto.general_store.ui.adapters.GoodsListAdapter;
import com.aletto.general_store.utils.Login;
import com.aletto.general_store.utils.ToastUtils;
import com.aletto.general_store.utils.XHelper;
import com.aletto.general_store.utils.image.GlideApp;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.sisyphus.spinach.core.INetPageTask;
import com.sisyphus.spinach.core.INetTask;
import com.sisyphus.spinach.core.model.IPageResponse;
import com.sisyphus.spinach.core.model.IResponse;
import com.sisyphus.spinach.network.presenters.NetPageTask;
import com.sisyphus.spinach.network.presenters.NetSingleTask;
import com.sisyphus.spinach.network.views.Debounce;
import com.sisyphus.spinach.ui.configs.impl.DefaultPageTemplate;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import io.reactivex.rxjava3.core.Observable;

/**
 * 店铺
 */
public class StoreDetailsActivity extends AppCompatActivity {

    public static Intent newIntent(Context context, String storeId) {
        return new Intent(context, StoreDetailsActivity.class).putExtra(Goods.EXTRA_NAME, storeId);
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_store);

        findViewById(R.id.back_image).setOnClickListener(v -> finish());
        ImageView favoritesStore = findViewById(R.id.check_favorites_store);
        RecyclerView recyclerView = findViewById(com.sisyphus.spinach.R.id.recycler_view);
        SmartRefreshLayout refreshLayout = findViewById(com.sisyphus.spinach.R.id.refresh_layout);

        // 门店详情
        Intent intent = getIntent();
        String storeId = intent.getStringExtra(Goods.EXTRA_NAME);
        storeDetailsTemplate.setParams(storeId).execute(getLifecycle());

        // 收藏门店
        favoritesStore.setOnClickListener(new Login(v ->
                favoritesStoreTemplate.setParams(storeId).execute(getLifecycle())));

        // 门店下的商品
        new StoreGoodsPageTemplate(recyclerView, refreshLayout).setParams(storeId).execute(getLifecycle());

        // 复制
        findViewById(R.id.iv_copy).setOnClickListener(v -> {
            XHelper.copy2Clipboard(String.valueOf(storeId));
            ToastUtils.makeText(R.string.copy_success);
        });
    }


    private final SimpleParamsTemplate<Store, String> storeDetailsTemplate = new SimpleParamsTemplate<>() {

        @Override
        public INetTask.View<Store> view() {
            return new INetTask.View<>() {
                @Override
                public void success(Store data) {
                    TextView titleText = findViewById(R.id.tv_title);
                    ImageView logoImage = findViewById(R.id.iv_store_logo);
                    TextView favoritesNumberText = findViewById(R.id.tv_favorites_number);
                    ImageView favoritesStore = findViewById(R.id.check_favorites_store);

                    titleText.setText(data.getName());
                    favoritesStore.setSelected(data.is_like == 1);
                    favoritesNumberText.setText(String.format(getString(R.string.store_favorites_number), data.like_num));
                    GlideApp.with(logoImage).load(data.getHead_img()).avatar().into(logoImage);
                }
            };
        }

        @Override
        public INetTask.Presenter<Store, INetTask.View<Store>> presenter() {
            return new NetSingleTask<>(DataRepository.getInstance().getShopDetails(params[0], 0));
        }
    };

    private final SimpleParamsTemplate<Object, String> favoritesStoreTemplate = new SimpleParamsTemplate<>() {

        @Override
        public INetTask.View<Object> view() {
            ImageView favoritesStore = findViewById(R.id.check_favorites_store);
            return new Debounce<>(favoritesStore, new INetTask.View<>() {
                @Override
                public void success(Object data) {
                    favoritesStore.setSelected(!favoritesStore.isSelected());
                }
            });
        }

        @Override
        public INetTask.Presenter<Object, INetTask.View<Object>> presenter() {
            ImageView favoritesStore = findViewById(R.id.check_favorites_store);
            return new NetSingleTask<>(DataRepository.getInstance().favoritesStore(params[0], !favoritesStore.isSelected()));
        }
    };

}
